package com.huluobo.a2210mdmdemo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Send
import androidx.compose.material3.Button
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedIconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.SPUtils
import com.huluobo.a2210mdmdemo.demo.Demo01
import com.huluobo.a2210mdmdemo.demo.Demo02
import com.huluobo.a2210mdmdemo.demo.Demo03
import com.huluobo.a2210mdmdemo.demo.Demo04
import com.huluobo.a2210mdmdemo.demo.Demo05
import com.huluobo.a2210mdmdemo.demo.Demo06
import com.huluobo.a2210mdmdemo.ui.theme._2210MDMDemoTheme
import com.huluobo.a2210mdmdemo.views.MainPage
import com.huluobo.a2210mdmdemo.views.WelcomePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
            ), 102
        )

        setContent {
            _2210MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = RoutePath.WELCOME
                    val mmkv = MMKV.defaultMMKV()
                    val navHostController = rememberNavController()

                    //通过mmkv去读取是否已经进入过app,如果读取不到就是false
                    val isIn = mmkv.decodeBool(Const.PARAM_IS_IN, false)
                    if (isIn) {
                        //如果进入过app,就直接跳转到首页
                        startPage = RoutePath.MAIN
                    } else {
                        //如果首次进入app,进入欢迎页并且倒计时5秒,再次进入首页
                        lifecycleScope.launch {
                            //延时5秒
                            async {
                                delay(5000)
                            }.await()
                            //跳转到首页
                            navHostController.navigate(RoutePath.MAIN)
                            //保存是否进入的值
                            mmkv.encode(Const.PARAM_IS_IN, true)
                        }
                    }

                    NavHost(navController = navHostController, startDestination = startPage, builder = {
                        composable(RoutePath.WELCOME) { WelcomePage() }
                        composable(RoutePath.MAIN) { MainPage() }
                    })
                }
            }
        }
    }
}
